package com.drinks.yiniang.ui.activity.community;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.community.CommunityFollowAdapter;
import com.drinks.yiniang.common.recycler.decoration.ItemSpacesDecoration;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.entity.community.CommunityNewsJson;
import com.drinks.yiniang.io.api.CommunityApiIO;
import com.drinks.yiniang.ui.widget.dialog.ShareDialog;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.hansen.library.ui.widget.refresh.MSwipeRefreshLayout;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;

/**
 * 社区动态(我的)
 * @author han
 * @date 2019/09/10
 */
public class CommunityNewsActivity extends BaseTranBarActivity implements OnNavgationBarClickListener,
        SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener,
        BaseQuickAdapter.RequestLoadMoreListener {

    //nav
    private NavigationBarLayout nav_bar_community_news;
    private MSwipeRefreshLayout refresh_community_news;
    //动态列表
    private BaseRecyclerView rcv_community_news;

    private CommunityFollowAdapter mFollowAdapter;

    private int mPages;

    @Override
    protected void initView() {
        nav_bar_community_news = findViewById(R.id.nav_bar_community_news);

        refresh_community_news = findViewById(R.id.refresh_community_news);

        rcv_community_news = findViewById(R.id.rcv_community_news);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        initAdapter();
        getCommunityNews();
    }

    private void initAdapter() {
        rcv_community_news.setLayoutManager(new LinearLayoutManager(mContext));
        rcv_community_news.addItemDecoration(ItemSpacesDecoration.getItemDPDecoration(10));

        mFollowAdapter = new CommunityFollowAdapter(mContext, new ArrayList<CommunityNewsJson.CommunityNewsList>());
        mFollowAdapter.setCenterEmpty(getLayoutInflater(), rcv_community_news);
        mFollowAdapter.bindToRecyclerView(rcv_community_news);
    }

    @Override
    protected void initListener() {
        nav_bar_community_news.setOnNavgationBarClickListener(this);
        refresh_community_news.setOnRefreshListener(this);

        mFollowAdapter.setOnItemChildClickListener(this);
        mFollowAdapter.setOnLoadMoreListener(this, rcv_community_news);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_community_news;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }


    @Override
    public void onRefresh() {
        getCommunityNews();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        CommunityNewsJson.CommunityNewsList data = mFollowAdapter.getItem(position);

        if(data == null) {
            return;
        }

        Intent mIntent;

        switch (view.getId()) {
            case R.id.iv_community_follow_avatar://头像
            case R.id.tv_community_follow_nickname://昵称
                mIntent = new Intent(mContext, UserCommunityActivity.class);
                mIntent.putExtra(Constants.KeyUserId, data.getFans_id());
                startActivity(mIntent);
                break;
            case R.id.iv_community_follow_three_dot://举报分享
                ShareDialog dialog = ShareDialog.newInstance(data.getFans_id(), data.getId(), true, true);

                dialog.show(getSupportFragmentManager(), Constants.TAG_SHARE_DIALOG);
                break;
            case R.id.tv_community_follow_collect://收藏心愿
                doCollect(data, position);
                break;
            case R.id.tv_community_follow_like://点赞
                doLike(data, position);
                break;
            case R.id.iv_community_follow_comments_avatar://评论
            case R.id.tv_community_follow_comments_add:
                mIntent = new Intent(mContext, CommunityCommentsActivity.class);
                mIntent.putExtra(Constants.KeyCommunityId, data.getId());
                startActivity(mIntent);
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onLoadMoreRequested() {
        CommunityApiIO.getInstance().getMyCommunityNew(mPages, new APIRequestCallback<CommunityNewsJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(CommunityNewsJson res) {
                if(mFollowAdapter == null) {
                    return;
                }

                mPages++;
                mFollowAdapter.addData(res.getData().getData());

                if (CommonUtils.isHasMoreData(res.getData().getData())) {
                    mFollowAdapter.loadMoreComplete();
                } else {
                    mFollowAdapter.loadMoreEnd();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mFollowAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
            }
        });
    }

    //获取社区动态
    private void getCommunityNews() {
        if(!refresh_community_news.isRefreshing()) {
            showLoadingDialog();
        }

        mPages = 1;

        CommunityApiIO.getInstance().getMyCommunityNew(mPages, new APIRequestCallback<CommunityNewsJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(CommunityNewsJson res) {
                if(mFollowAdapter == null) {
                    return;
                }

                mPages++;
                mFollowAdapter.getData().clear();
                mFollowAdapter.addData(res.getData().getData());

                if (CommonUtils.isHasMoreData(res.getData().getData())) {
                    mFollowAdapter.loadMoreComplete();
                } else {
                    mFollowAdapter.loadMoreEnd();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mFollowAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
                refresh_community_news.setRefreshing(false);
                dismissDialog();
            }
        });
    }

    private void doLike(final CommunityNewsJson.CommunityNewsList data, final int pos) {
        if (StringUtils.isEmpty(data.getId())) {
            ToastUtils.showShort(R.string.text_community_not_exist);
            return;
        }

        showLoadingDialog();

        CommunityApiIO.getInstance().doCommunityLike(data.getId(), new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                if(CommonUtils.isArrayIndexOutOfBounds(mFollowAdapter.getData(), pos)) {
                    return;
                }

                data.setLike_count(res.getData());
                data.setIs_like(Constants.STATUS_GLOBAL_YES.equals(data.getIs_like()) ? Constants.STATUS_GLOBAL_NO : Constants.STATUS_GLOBAL_YES);

                mFollowAdapter.setData(pos, data);
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    private void doCollect(final CommunityNewsJson.CommunityNewsList data, final int pos) {
        if (StringUtils.isEmpty(data.getId())) {
            ToastUtils.showShort(R.string.text_community_not_exist);
            return;
        }

        showLoadingDialog();

        CommunityApiIO.getInstance().doCommunityCollect(data.getId(), new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                if(CommonUtils.isArrayIndexOutOfBounds(mFollowAdapter.getData(), pos)) {
                    return;
                }

                data.setCollect_count(res.getData());
                data.setIs_collect(Constants.STATUS_GLOBAL_YES.equals(data.getIs_collect()) ? Constants.STATUS_GLOBAL_NO : Constants.STATUS_GLOBAL_YES);

                mFollowAdapter.setData(pos, data);
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
